package com.fafa.pos.activity;

import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.fafa.pos.R;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.ICardInfoDao;
import com.fafa.pos.dao.IConsumeInfoDao;
import com.fafa.pos.dao.IVipInfoDao;
import com.fafa.pos.dao.impl.CardInfoDaoImpl;
import com.fafa.pos.dao.impl.ConsumeInfoDaoImpl;
import com.fafa.pos.dao.impl.VipInfoDaoImpl;
import com.fafa.pos.pojo.CardInfo;
import com.fafa.pos.pojo.ConsumeInfo;
import com.fafa.pos.pojo.VipInfo;
import com.fafa.pos.util.MyActivityManager;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GatheringDetailActivity extends TitleActivity {


    //dao相关
    private DBUtil dbUtil;
    private IConsumeInfoDao consumeInfoDao;
    private IVipInfoDao vipInfoDao;
    private ICardInfoDao cardInfoDao;

    //view相关
    private TextView totalCount;
    private ListView gatheringDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gathering_detail);

        //设置状态栏和标题栏同色
        Window window = this.getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.LoginBlue));

        ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }


        //加入activity管理
        MyActivityManager.getInstance().addActivity(this);

        setTitle("今日收款明细");
        showBackwardView(R.string.text_back,true);
        showForwardView(R.string.text_save,false);


        initDao();
        initView();
        initEvent();

    }

    private void initDao() {
        dbUtil = new DBUtil(getApplicationContext().getFilesDir().getAbsoluteFile()+"/pos.db");
        consumeInfoDao = new ConsumeInfoDaoImpl(dbUtil);
        vipInfoDao = new VipInfoDaoImpl(dbUtil);
        cardInfoDao = new CardInfoDaoImpl(dbUtil);
    }

    private void initView() {
        totalCount = (TextView) findViewById(R.id.totalCount);
        gatheringDetail = (ListView) findViewById(R.id.gatheringDetail);


        Long startTime = System.currentTimeMillis();

        double total = 0.00;
        List<Map<String,String>> data = new ArrayList<Map<String, String>>();
        List<ConsumeInfo> consumeInfos = consumeInfoDao.getCurDayConsumeInfo();
        if(consumeInfos!=null){
            for (ConsumeInfo consumeInfo:consumeInfos) {
                Map<String,String> dataItem = new HashMap<String,String>();
                String type = "1".equals(consumeInfo.getIsUpLoad())?"在线消费 : ":"离线消费 : ";
                dataItem.put("money",type+consumeInfo.getMoney()+"元");
                dataItem.put("vipName",consumeInfo.getVipName());
                dataItem.put("cardCode",consumeInfo.getCardCode());
                dataItem.put("date",consumeInfo.getDate());
                data.add(dataItem);

                total+= Double.parseDouble(consumeInfo.getMoney());
            }
            DecimalFormat df = new DecimalFormat("0.00");
            totalCount.setText(consumeInfos.size()+"条,合计"+df.format(total)+"元");
        }
        //实例化适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(GatheringDetailActivity.this,data,R.layout.garhering_detail_item,new String[]{"money","vipName","cardCode","date"},new int[]{R.id.money,R.id.vipName,R.id.cardCode,R.id.date});
        //ListView 加载适配器
        gatheringDetail.setAdapter(simpleAdapter);
        System.out.println("加载今日收款明细"+(System.currentTimeMillis()-startTime)+"ms");

    }

    private void initEvent() {
    }
}
